package com.bang.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: SynchronizedExample1
 * @Desc: 演示synchronized关键字作用范围
 * @auther: BANG
 * @Date: 2019/09/30 12:51
 * @Version: 1.0
 * @Modified By:
 * <p>
 * 原子性-对比
 * synchronized: 不可中断锁,适用于竞争不激烈的场景.可读性好
 * Lock: 可中断锁,多样化同步,竞争激烈仍可保持常态
 * atomic: 竞争激烈仍可保持常态,性能优于lock;但是只能同步一个值
 */
@Slf4j
public class SynchronizedExample1 {


    // 修饰代码块
    public void test1(int j) {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                log.error("Thread {} {} test1 --> {}", Thread.currentThread().getName(), j, i);
            }
        }
    }


    // 修饰方法
    public synchronized void test2(int j) {
        for (int i = 0; i < 10; i++) {
            log.error("Thread {} {} test1 --> {}", Thread.currentThread().getName(), j, i);
        }
    }

    // 修饰静态方法
    public synchronized static void test3(int j) {
        for (int i = 0; i < 10; i++) {
            log.error("Thread {} {} test4 --> {}", Thread.currentThread().getName(), j, i);
        }
    }


    // 修饰类
    public static void test4(int j) {
        synchronized (SynchronizedExample1.class) {
            for (int i = 0; i < 10; i++) {
                log.error("Thread {} {} test1 --> {}", Thread.currentThread().getName(), j, i);
            }
        }
    }

    public static void main(String[] args) {
//        testSingleInstance();
//        testTwoInstance();
//        testStatic();
        testClass();
    }

    // 测试单个对象
    private static void testSingleInstance() {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test2(1);
        });

        executorService.execute(() -> {
            example1.test2(1);
        });
    }


    // 测试多个对象
    private static void testTwoInstance() {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1();
        SynchronizedExample1 example3 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test2(1);
        });

        executorService.execute(() -> {
            example2.test2(2);
        });
        executorService.execute(() -> {
            example3.test2(3);
        });
    }


    // 测试静态方法
    private static void testStatic() {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test3(1);
        });
        executorService.execute(() -> {
            example2.test3(2);
        });

    }

    // 测试类
    private static void testClass() {
        SynchronizedExample1 example1 = new SynchronizedExample1();
        SynchronizedExample1 example2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            example1.test4(1);
        });
        executorService.execute(() -> {
            example2.test4(2);
        });
    }


}
